package com.cn.hnust.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 
* @ClassName: ClassUtils
* @Package com.handlecar.erp.util 
* @Description: TODO  java反射机制帮助类
* @author hc_wangjun 
* @date 2014年7月28日 上午10:23:03
 */
public class ClassUtils {

	/**
	 * 
	* @Title: getGetMethod 
	* @Description: TODO 获取get方法
	* @param objectClass
	* @param fieldName 属性
	* @return
	* @throws
	 */
	public static Method getGetMethod(Class<?> objectClass, String fieldName) {
		StringBuffer sb = new StringBuffer();

		sb.append("get");
		// 截取
		sb.append(fieldName.substring(0, 1).toUpperCase());

		sb.append(fieldName.substring(1));

		try {

			return objectClass.getMethod(sb.toString());

		} catch (Exception e) {

		}

		return null;

	}

	/**
	 * 
	* @Title: getSetMethod 
	* @Description: TODO 获取set方法
	* @param objectClass
	* @param fieldName 属性
	* @return
	* @throws
	 */
	public static Method getSetMethod(Class<?> objectClass,String fieldName,Class[] parameterTypes) {

		try {
			

			StringBuffer sb = new StringBuffer();

			sb.append("set");

			sb.append(fieldName.substring(0, 1).toUpperCase());

			sb.append(fieldName.substring(1));

			Method method = objectClass
					.getMethod(sb.toString(), parameterTypes);

			return method;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;

	}

	/**
	 * 
	* @Title: invokeSet 
	* @Description: TODO 调用set
	* @param o
	* @param fieldName
	* @param value
	* @throws
	 */
	public static void invokeSet(Object o, String fieldName, Object value) {

		if(o == null) {
			return;
		}
	 try {
		 
		@SuppressWarnings("rawtypes")
		Class[] parameterTypes = new Class[1];

		Field field = o.getClass().getDeclaredField(fieldName);			

		parameterTypes[0] = field.getType();		
		
		Method method = getSetMethod(o.getClass(), fieldName,parameterTypes);		
	
		if(parameterTypes[0]==Integer.class)
		{
			if (value instanceof Integer) {
					value=Integer.parseInt(value.toString());
				}else
				{
					Float f=Float.parseFloat(value.toString());
					value=f.intValue();
				}			
			
		}else if(parameterTypes[0]==Float.class)
		{
			value=Float.parseFloat(value.toString());
		}else{
			value=value.toString();
		}
		
		method.invoke(o, new Object[] { value });

		} catch (Exception e) {

			e.printStackTrace();

		}

	}

	/**
	 * 
	* @Title: invokeGet 
	* @Description: TODO 调用get
	* @param o
	* @param fieldName
	* @return
	* @throws
	 */
	public static Object invokeGet(Object o, String fieldName) {
		
		if(o == null) {
			return null;
		}

		Method method = getGetMethod(o.getClass(), fieldName);

		try {

			return method.invoke(o, new Object[0]);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;

	}
	
	/**
	 * 
	* @Title: getDeclaredFields 
	* @Description: TODO 获取类中所有的属性
	* @param obj
	* @return
	* @throws
	 */
	public static Field[] getDeclaredFields(Object obj) {
		
		if(obj == null) {
			return null;
		}
		return obj.getClass().getDeclaredFields();
	}

}